package top.hidoctor.auscultation.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


import java.util.ArrayList;
import java.util.List;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.bean.BrokenLineCusVisit;


/**
 * Created by Administrator on 2015/10/13.
 */
public class MyEcgView extends View {
    private int widths;
    private int height;

    //网格的宽度与高度
    private float gridspace_width;
    private float gridspace_heigh;
    //底部空白的高度
    private int brokenline_bottom;


    //灰色背景的画笔
    private Paint mPaint_bg;
    //灰色网格的画笔
    private Paint mPaint_gridline;

    //折线圆点的蓝色背景
    private Paint mPaint_point_bg;
    //折线圆点的白色表面
    private Paint mPaint_point_sur;
    //阴影路径的画笔
    private Paint mPaint_path;
    //折线的画笔
    private Paint mPaint_brokenline;
    //路径
    private Path mpath = new Path();
    //客户拜访的折线（BrokenLineCusVisit）数据
    private List<Float> mdata = new ArrayList<>();
    private int dataNum_per_grid = 500;//每小格内的数据个数
    private float gap_x;//两点间横坐标间距
    private int xori;//原点x坐标
    private int grid_hori, grid_ver;//横、纵线条数
    private int xLine;


    public int getWidths() {
        return widths;
    }

    public MyEcgView(Context context) {
        super(context);
    }

    public MyEcgView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inite(context);
    }


    private void inite(Context context) {
        mPaint_bg = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_bg.setColor(Color.argb(0xff, 0xef, 0xef, 0xef));

        mPaint_gridline = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_gridline.setColor(Color.argb(0xff, 0xce, 0xCB, 0xce));

        mPaint_brokenline = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_brokenline.setColor(context.getResources().getColor(R.color.blue_main));
        mPaint_brokenline.setTextSize(18);
        mPaint_brokenline.setTextAlign(Paint.Align.CENTER);

        mPaint_point_bg = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_point_bg.setColor(Color.argb(0xff, 0x91, 0xC8, 0xD6));

        mPaint_path = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_path.setColor(Color.argb(0x33, 0x91, 0xC8, 0xD6));


        mPaint_point_sur = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_point_sur.setColor(Color.WHITE);


        invalidate();
    }

    public List<Float> getMdata() {
        return mdata;
    }

    public void setMdata(List<Float> mdata) {
        this.mdata = mdata;
        requestLayout();//相当于调用onMeasure方法
        invalidate();//相当于调用onDraw方法
    }


    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        //绘制折线图的白色背景
        canvas.drawColor(Color.WHITE);
        //绘制灰色矩形区域
        canvas.drawRect(10, 0, widths, height - brokenline_bottom, mPaint_bg);


        for (int j = 0; j < xLine; j++) {
            canvas.drawLine(0, gridspace_width * (j + 1), widths, gridspace_width * (j + 1), mPaint_gridline);
        }


        for (int i = 0; i < mdata.size(); i++) {
            if (i == 0) {
                //开始时需要将path移动到要开始绘制的位置，否则默认从（0,0）开始，绘制path路径
//                mpath.moveTo(gridspace_width * i + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i).getData() / 100);
                mpath.moveTo(0, height - brokenline_bottom - (height - brokenline_bottom) * mdata.get(i));
            }
//            //绘制纵向网格线
            canvas.drawLine(gridspace_width * i + 10, 0, gridspace_width * i + 10, height - brokenline_bottom, mPaint_gridline);
            if (i != mdata.size() - 1) {
                //根据圆点位置绘制折线
//                canvas.drawLine(gridspace_width * i + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i).getData() / 100, gridspace_width * (i + 1) + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i + 1).getData() / 100, mPaint_brokenline);
                canvas.drawLine(gap_x * i + 1, height - brokenline_bottom - (height - brokenline_bottom) * mdata.get(i), xori + gap_x * (i + 1) + 1, height - brokenline_bottom - (height - brokenline_bottom) * mdata.get(i + 1), mPaint_brokenline);
                //path的路径跟绘制的线的路径是相同的，因此path的起止点与线的起止点相同
//                mpath.quadTo(gridspace_width * i + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i).getData() / 100, gridspace_width * (i + 1) + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i + 1).getData() / 100);
                mpath.quadTo(xori + gap_x * i + 1, height - brokenline_bottom - (height - brokenline_bottom) * mdata.get(i), xori + gap_x * (i + 1) + 1, height - brokenline_bottom - (height - brokenline_bottom) * mdata.get(i + 1));
            }

            //绘制底部空白处：数据的日期
//            canvas.drawText(date, gap_x * i + 1, heigh - brokenline_bottom / 2, mPaint_text);

            if (i == mdata.size() - 1) {
                //绘制完最后一个点，path需要沿着纵轴向下到达heigh - brokenline_bottom 的位置再水平到达（10，heigh - brokenline_bottom ）的位置，最后闭合
                mpath.quadTo(gridspace_width * i + 10, height - brokenline_bottom - (height - brokenline_bottom) * mdata.get(i), gridspace_width * i + 10, height - brokenline_bottom);
                mpath.quadTo(gridspace_width * i + 10, height - brokenline_bottom, 10, height - brokenline_bottom);
                mpath.close();
            }
        }
        //最终在画布上绘制path
        canvas.drawPath(mpath, mPaint_path);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        gridspace_width = 20f;
        gap_x = gridspace_width / dataNum_per_grid;
        xori = 0;//原点x坐标
        if (mdata.size() == 0) {
            //通过调用onMeasure源码中的方法进行获得宽度，
            //宽度的获得有三种模式，具体介绍见博客底部连接
            widths = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        } else {
            //根据数据条数设置宽度
            widths = (int) (gap_x * mdata.size());
        }

//        heigh=getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        height = 400;
        grid_hori = height / (int) gridspace_width;//横、纵线条数
        grid_ver = widths / (int) gridspace_width;


//        Log.e("TAG", "onMeasure: "+heigh );
        //设置底部白色空白的宽度
        brokenline_bottom = 0;
//        //绘制网格线，横向的；Y轴不变 X轴绘制直线
        xLine = height / (int) gridspace_width; //橫軸條數

        gridspace_heigh = (height - brokenline_bottom) / xLine;
        setMeasuredDimension(widths, height);
    }


}